package LeetCode.linkednode;

public class NC_BM13 {
    public Boolean isPail(ListNode head) {
        ListNode fast = head;
        ListNode slow = head;
        while (fast != null && fast.next != null) {  // 找中间节点
            fast = fast.next.next;
            slow = slow.next;
        }
        ListNode temp = slow.next;  // 需要反转的节点
        while (temp != null) {  // 将中间节点后面的节点反转
            ListNode tempNext = temp.next;
            temp.next = slow;
            slow = temp;
            temp = tempNext;
        }
        while (slow != head) {  // 判断反转后是否为回文链表
            if (slow.val != head.val) {
                return false;
            }
            if (head.next == slow){  // 节点为偶数情况下  // == ? !=
                return true;
            }
            slow = slow.next;
            head = head.next;
        }
        return true;
    }
}
